<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2025/5/19/019
 * Time: 16:47
 */

namespace app\common\service;

use think\facade\Log;

class BzhanService
{
    private $url = 'https://account.bilibili.com';
    protected $appid;

    protected $appsecret;
    public function __construct()
    {
        $set = sysconfig('forward');
        $this->appid = empty($set['k_appid']) ? '' : $set['k_appid'];
        $this->appsecret = empty($set['k_secret']) ? '' : $set['k_secret'];

    }

    public function BzhanQrcode($uid)
    {
        $url = $this->url . '/oauth2/authorize?';
        $request = Request();
        $domain = $request->host();
        $callbackUrl = urlencode("https://" . $domain . "/mobile/ExternalAccount/kuaishouQrcodeRedirect?uid=".$uid);
        $url = "https://open.kuaishou.com/oauth2/authorize?app_id=".$this->appid ."&response_type=code&scope=user_info,user_video_publish,user_video_info&redirect_uri=" . $callbackUrl;
        $qrcode = common_qr_code($url, 'repost');
        if ($qrcode) {
            return array(
                'code' => 1,
                'data' => $qrcode,
                'msg' => 'success'
            );
        } else {
            return array(
                'code' => 0,
                'data' => [],
                'msg' => '获取失败'
            );

        }
    }

    function apiPost($url, $header, $data = NULL)
    {
        $request = Request();
        $domain = $request->host();

        $headers = array(
            'Content-Type: application/json; charset=utf-8'
        );
        if (!empty($header)) {
            $headers = array_merge($headers, $header);
        }

        $send_data = array(
            'appid' => sysconfig('account', 'account_appid'),
            'appsecret' => sysconfig('account', 'account_appsecret'),
            'domain' => $domain
        );

        if (!empty($data)) {

            if (!is_array($data)) {
                $data = $data->toArray();
            }

            $send_data = array_merge($send_data, $data);
        }

        $userAgent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

        // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($send_data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 600); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 类型为json
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Error POST' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $result; // 返回数据
    }

    function httpGet($url, $header = [])
    {
        $request = Request();
        $domain = $request->host();

        $headers = array(
            'Content-Type: application/json; charset=utf-8'
        );
        if (!empty($header)) {
            $headers = array_merge($headers, $header);
        }

        // 初始化
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 类型为json
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        // 执行并获取HTML文档内容
        $output = curl_exec($ch);

        // 释放curl句柄
        curl_close($ch);

        return $output;
    }
}